package com.sharemarking.application.common.dto;

import java.util.ArrayList;
import java.util.List;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.sharemarking.application.common.entity.Role;
import com.sharemarking.application.common.entity.User;

public class UserDetailImpl implements UserDetails {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String username;
	private String password;
	private String name;
	private boolean loginFlag;
	private List<SimpleGrantedAuthority> list;
	
	public UserDetailImpl(User user) {
		this.id=user.getId();
		this.username=user.getLoginName();
		this.password=user.getPassword();
		this.loginFlag=Boolean.TRUE.equals(user.getLoginFlag());
		this.list=new ArrayList<SimpleGrantedAuthority>();
		for(Role role:user.getRoles()){
			this.list.add(new SimpleGrantedAuthority("ROLE_"+role.getEname()));
		}
		name=user.getName();
	}

	@Override
	public List<SimpleGrantedAuthority> getAuthorities() {
		return list;
	}
	
	public Integer getId(){
		return id;
	}
	
	public String getName(){
		return name;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return !this.loginFlag;
	}
	
}
